package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: ApartmentInfoServiceImpl
 * Package: com.atguigu.lease.web.admin.service.impl
 * Description:
 *
 * @Author ygf
 * @Create 2024/9/14 13:54
 * @Version 1.0
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper,ApartmentInfo> implements ApartmentInfoService {

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    ApartmentLabelService apartmentLabelService;
    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;

    @Override
    public void saveOrUpdateApartmentInfo(ApartmentSubmitVo apartmentSubmitVo) {
        //获取公寓id，有则为更新无则是添加
        Long id = apartmentSubmitVo.getId();//TODO 思考提出的id问题
        if (id != null){//即为更新，在此删除中间表的数据(更新思路：简单暴力实现先删除，再插入)
            //1、删除公寓配套信息（根据公寓id）
            LambdaQueryWrapper<ApartmentFacility> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApartmentFacility::getApartmentId,id);
            apartmentFacilityService.remove(queryWrapper);
            //2、删除公寓标签信息(根据公寓id)
            LambdaQueryWrapper<ApartmentLabel> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ApartmentLabel::getApartmentId,id);
            apartmentLabelService.remove(queryWrapper1);
            //3.同理，删除公寓杂费信息(根据公寓id)
            LambdaQueryWrapper<ApartmentFeeValue> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(ApartmentFeeValue::getApartmentId,id);
            apartmentFeeValueService.remove(queryWrapper2);

            //4.删除公寓图片信息(根据公寓id 和 图片类型)
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
        }

        //调用service层的保存或更新方法---->保存公寓信息
        this.saveOrUpdate(apartmentSubmitVo);//TODO 卡断点看看this是谁？

        //1、保存公寓配套信息____获取DTO里所有的公寓配套id
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        //遍历所有id
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            //建集合
            ArrayList<ApartmentFacility> apartmentFacilityArrayList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                //创建要插入的中间表对象并设置属性值
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().facilityId(facilityInfoId).apartmentId(apartmentSubmitVo.getId()).build();
                //将创建好的apartmentFacility对象添加到集合中
                apartmentFacilityArrayList.add(apartmentFacility);
            }
            //批量插入
            apartmentFacilityService.saveBatch(apartmentFacilityArrayList);
        }

        //2、保存公寓标签信息____获取DTO里所有的公寓标签id
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        //遍历所有id
        if(!CollectionUtils.isEmpty(labelIds)){
            //建集合
            ArrayList<ApartmentLabel> apartmentLabelArrayList = new ArrayList<>();
            for (Long labelId : labelIds) {
                //创建要插入的中间表对象并设置属性值
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().labelId(labelId).apartmentId(apartmentSubmitVo.getId()).build();
                //添加到集合中
                apartmentLabelArrayList.add(apartmentLabel);
            }
            //批量插入
            apartmentLabelService.saveBatch(apartmentLabelArrayList);
        }

        //3、保存公寓杂费值信息___获取DTO里的所有公寓杂费值id
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        //遍历所有id
        if (!CollectionUtils.isEmpty(feeValueIds)){
            //构建集合  //TODO 集合能写到if外面吗？
            ArrayList<ApartmentFeeValue> feeValueArrayList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                //创建要插入的中间表对应的对象并设置属性值
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().feeValueId(feeValueId).apartmentId(apartmentSubmitVo.getId()).build();
                //添加到集合中
                feeValueArrayList.add(apartmentFeeValue);
            }
            //批量插入
            apartmentFeeValueService.saveBatch(feeValueArrayList);
        }

        //有所不同
        //4、保存公寓图片信息_____获取DTO里所有的图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            //建集合
            ArrayList<GraphInfo> graphInfoArrayList = new ArrayList<>();
            //遍历图片集合
            for (GraphVo graphVo : graphVoList) {
                //创建要插入的表对应的对象并设置属性值
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(apartmentSubmitVo.getId());//公寓id
                graphInfo.setItemType(ItemType.APARTMENT);//枚举
                //添加到集合中
                graphInfoArrayList.add(graphInfo);
            }
            //批量插入
            graphInfoService.saveBatch(graphInfoArrayList);
        }
    }

    @Override
    public void deleteApartmentInfoById(Long id) {
        //调用Mp的根据id删除的方法
        this.removeById(id);
        //删除中间表的数据
        //1、删除公寓配套信息（根据公寓id）
        LambdaQueryWrapper<ApartmentFacility> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(queryWrapper);
        //2、删除公寓标签信息(根据公寓id)
        LambdaQueryWrapper<ApartmentLabel> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(queryWrapper1);
        //3.同理，删除公寓杂费信息(根据公寓id)
        LambdaQueryWrapper<ApartmentFeeValue> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(queryWrapper2);

        //4.删除公寓图片信息(根据公寓id 和 图片类型)
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
    }


    @Override
    public IPage<ApartmentItemVo> getPageList(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return baseMapper.getPageList(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        //调用MyBatis Plus中根据id查询的方法获取公寓信息
        ApartmentInfo apartmentInfo = getById(id);

        //根据公寓id和item_type查询公寓的图片
        List<GraphVo> graphVoList = graphInfoMapper.getApartmentPicsByItemIdAndItemType(id,ItemType.APARTMENT);
        //根据公寓id查询公寓标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.getLabelInfoByApartmentId(id);
        //根据公寓id查询公寓的配套列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getFacilityInfoByApartmentId(id);
        //根据公寓id查询公寓的杂费列表
        List<FeeValueVo> feeValueVoList = feeValueMapper.getFeeValueVoByApartmentId(id);
        //创建ApartmentDetailVo对象
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        //设置公寓的图片
        apartmentDetailVo.setGraphVoList(graphVoList);
        //设置公寓的标签
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        //设置公寓的配套
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        //设置公寓的杂费
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }
}
